﻿
using JshRCSWebApi.Infrastructure.Domain.DTO.SysService;
using JshRCSWebApi.Util.Cache;
using JshRCSWebApi.Util.Cache.Redis;
using JshRCSWebApi.Util.DataAccess;

public class UserController : BaseController
{
    protected readonly UserService _userService;
    protected readonly JwtManager _tokenHelper;

    public UserController(UserService userService, IHttpContextAccessor httpContextAccessor, JwtManager tokenHelper)
        : base(httpContextAccessor)
    {
        _tokenHelper = tokenHelper;
        _userService = userService;
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="userLogin">用户账号、密码、卡号</param>
    /// <returns></returns>
    [HttpPost]
    [AllowAnonymous]
    public async Task<ApiResult> Login([FromBody] UserLoginDTO userLogin)
    {
        return await Task.Run(async () =>
        {
            ApiResult apiResult = await _userService.Login(userLogin.UserName, userLogin.PassWord, userLogin.CardNum);
            if (apiResult.code == 200)
            {
                serviceuser suEntity = (apiResult.data as serviceuser) ?? new serviceuser();
                if (!suEntity.Equals(new serviceuser()))
                {
                    string token = _tokenHelper.CreateJwtToken(new User() { ID = suEntity.ID, Name = suEntity.suName });
                    RedisFn.fn_redis_save_string($"user_{suEntity.ID}",$"{token},{DateTimeConvert.DateTimeToTimestamp(DateTime.Now)}");
                    RedisFn.fn_redis_setExpire($"user_{suEntity.ID}",TimeSpan.FromMinutes(3));
                    return ApiResult.Success($"欢迎{suEntity.suName}登录本系统！", new { token });
                }
            }
            return apiResult;
        });
    }

    /// <summary>
    /// 获取用户
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<ApiResult> GetUserInfo()
        => await Task.Run(async () => await _userService.GetUserInfo(Convert.ToInt32(GetUser("ID"))));

    /// <summary>
    /// 新增用户
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult> AddUser([FromBody] UserAddDTO addDTO)
      =>  await Task.Run(async () => await _userService.AddUser(addDTO, Convert.ToInt32(GetUser("ID"))));
    /// <summary>
    /// 修改用户
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult> UpdateUser([FromBody] UserUpdateDTO updateDTO)
        => await Task.Run(async () => await _userService.UpdateUser(updateDTO, Convert.ToInt32(GetUser("ID"))));

    /// <summary>
    /// 获取用户列表
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult> GetUserList([FromBody] UserSearchDTO searchDTO) =>
        await Task.Run(async () => ApiResult.Success("获取成功", await _userService.GetUserList(searchDTO)));

    /// <summary>
    /// 通过ID获取用户信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<ApiResult> GetUserInfoByID(long id) =>
        await Task.Run(async () => await _userService.GetUserInfoByID(id));

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<ApiResult> DeleteUser(long ID) =>
        await Task.Run(async() => await _userService.DeleteUser(ID));

    /// <summary>
    /// 修改用户密码
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="PWD"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult> UpdateUserPWD([FromBody] UserUpdatePwdDTO pwdDTO) =>
        await Task.Run(async() => await _userService.UpdateUserPWD(pwdDTO));

    /// <summary>
    /// 修改用户权限
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult> UpdateUserJurisdiction([FromBody] UserUpdateJurisdictionDTO updateDto)
        => await Task.Run(async () => await _userService.UpdateUserJurisdiction(updateDto, Convert.ToInt32(GetUser("ID"))));

    /// <summary>
    /// 获取用户和角色所有菜单权限
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<ApiResult> GetUserAndRoleAllJurisdiction()
        => await Task.Run(async () => await _userService.GetUserAndRoleAllJurisdiction(Convert.ToInt32(GetUser("ID"))));

    /// <summary>
    /// 用户退出
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult> LogOut()
    {
        return  await Task.Run(async () => await _userService.LogOut(Convert.ToInt32(GetUser("ID"))));
    }
}
